// pages/category/category.js
const app = getApp();

Page({
  data: {
    categories: [],
    resources: [],
    filteredResources: [],
    currentCategory: 'all',
    currentSystem: 'all'
  },
  
  onLoad() {
    console.log('category onLoad');
    // 获取全局分类和资源数据
    this.setData({
      categories: app.globalData.categories.filter(cat => cat.id !== 'all'),
      resources: app.globalData.resources,
      filteredResources: app.globalData.resources
    });
  },
  
  // 切换分类
  switchCategory(e) {
    const category = e.currentTarget.dataset.category;
    this.setData({ currentCategory: category });
    this.filterResources();
  },
  
  // 切换系统
  switchSystem(e) {
    const system = e.currentTarget.dataset.system;
    this.setData({ currentSystem: system });
    this.filterResources();
  },
  
  // 筛选资源
  filterResources() {
    let filtered = [...this.data.resources];
    
    // 按分类筛选
    if (this.data.currentCategory !== 'all') {
      filtered = filtered.filter(item => item.category === this.data.currentCategory);
    }
    
    // 按系统筛选
    if (this.data.currentSystem !== 'all') {
      filtered = filtered.filter(item => item.type === this.data.currentSystem);
    }
    
    this.setData({ filteredResources: filtered });
  },
  
  // 前往详情页
  goToDetail(e) {
    const id = e.currentTarget.dataset.id;
    console.log('goToDetail(e)');
    wx.navigateTo({
      url: `/pages/detail/detail?id=${id}`
    });
  },
  
  // 前往搜索页
  goToSearch() {
    console.log('goToSearch()');
    wx.navigateTo({
      url: '/pages/search/search'
    });
  },
  
  // 预览图片
  previewImage(e) {
    const url = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [url]
    });
  },
  
  // 分享功能
  onShareAppMessage() {
    return {
      title: '微信小程序资源分类',
      path: '/pages/category/category',
      imageUrl: '/images/share.jpg'
    }
  }
})
